/*
 * Copyright (c)  2008 Sebastien Tremblay
 *
 * id3-tag-builder - helpers.TreeHelper.java
 *
 * All source and documentation is copyrighted by Sebastien Tremblay
 * (seb at ryders dot net) and made available under the Apache License 2.0.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * $Id: CheckTreeCellRenderer.java 30 2010-01-08 04:01:06Z ryders $
 */
package com.empireone.id3tagbuilder.gui.tree;

import java.awt.BorderLayout;
import java.awt.Component;
import javax.swing.JPanel;
import javax.swing.JTree;
import javax.swing.tree.TreeCellRenderer;
import javax.swing.tree.TreePath;

/**
 *
 * @author Sebastien Tremblay
 */
public class CheckTreeCellRenderer extends JPanel implements TreeCellRenderer {

    private CheckTreeSelectionModel selectionModel;
    private TreeCellRenderer renderer;
    private TriStateCheckBox checkbox = new TriStateCheckBox();

    /**
     *
     * @param renderer
     * @param selectionModel
     */
    public CheckTreeCellRenderer(TreeCellRenderer renderer, CheckTreeSelectionModel selectionModel) {
        this.renderer = renderer;
        this.selectionModel = selectionModel;
        setLayout(new BorderLayout());
        setOpaque(false);
        checkbox.setOpaque(false);
    }

    /**
     *
     * @param tree
     * @param value
     * @param selected
     * @param expanded
     * @param leaf
     * @param row
     * @param hasFocus
     * @return
     */
    @Override
    public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
        Component c = renderer.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);

        TreePath path = tree.getPathForRow(row);
        if (path != null) {
            if (selectionModel.isPathSelected(path, true)) {
                checkbox.setState(TriStateCheckBox.State.CHECKED);
                //checkbox.setState(TriState.SELECTED);
            } else {
                checkbox.setState(selectionModel.isPartiallySelected(path) ? TriStateCheckBox.State.PARTIAL : TriStateCheckBox.State.UNCHECKED);
                //checkbox.setState(selectionModel.isPartiallySelected(path) ? TriState.MIXED : TriState.UNSELECTED);

            }
        }
        removeAll();
        add(checkbox, BorderLayout.WEST);
        add(c, BorderLayout.CENTER);
        return this;
    }

    /**
     *
     * @return
     */
    public TriStateCheckBox getRenderer() {
        return checkbox;
    }
}
